﻿using GlennQuigmire.API.Application.GDtos;
using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GlennQuigmire.API.Application.Commands
{
    public class InboundOrderDetailHander : IRequestHandler<InboundOrderDetailCommand, ResultT<InboundOrderDetailDto>>
    {


        private readonly IBaseRepositorys<InboundOrder> inboundOrderRepository;
        private readonly IBaseRepositorys<InboundOrderProduct> inboundOrderProductRepository;
        private readonly IBaseRepositorys<Product> productRepository;

        public InboundOrderDetailHander(IBaseRepositorys<InboundOrder> inboundOrderRepository, IBaseRepositorys<InboundOrderProduct> inboundOrderProductRepository, IBaseRepositorys<Product> productRepository)
        {
            this.inboundOrderRepository = inboundOrderRepository;
            this.inboundOrderProductRepository = inboundOrderProductRepository;
            this.productRepository = productRepository;
        }

        /// <summary>
        /// 反添入库单信息
        /// </summary>
        /// <param name="request">要反添的入库单主键</param>
        /// <param name="cancellationToken">允许取消异步</param>
        /// <returns>返回入库单信息</returns>
        public Task<ResultT<InboundOrderDetailDto>> Handle(InboundOrderDetailCommand request, CancellationToken cancellationToken)
        {
            try
            {

                var result= new ResultT<InboundOrderDetailDto>();
                var info = (from a in inboundOrderRepository.GetAll()
                            where a.InboundOrderId == request.InboundOrderId && !a.DeleteDate
                            select new InboundOrderDetailDto
                            {
                                InboundOrderId = a.InboundOrderId,
                                InboundOrderNumber = a.InboundOrderNumber,
                                InboundOrderTypeId = a.InboundOrderTypeId,
                                Remark = a.Remark,
                                InboundOrderDate = a.InboundOrderDate,
                                InboundOrderProducts = new List<InboundOrderProductsDto>()
                            }).FirstOrDefault();

                if (info != null)
                {
                    info.InboundOrderProducts = (from a in inboundOrderProductRepository.GetAll().Where(c => c.InboundOrderId == request.InboundOrderId && !c.DeleteDate)
                                                 join b in productRepository.GetAll() on a.ProductId equals b.ProductId
                                                 select new InboundOrderProductsDto
                                                 {
                                                     ProductId = a.ProductId,
                                                     ProductNumber = b.ProductNumber,
                                                     ProductName = b.ProductName,
                                                     ProductSpecification = b.ProductSpecification,
                                                     StockUnit = b.StockUnit == 1 ? "条" : b.StockUnit == 2 ? "张" : b.StockUnit == 3 ? "车" : b.StockUnit == 4 ? "车" : b.StockUnit == 5 ? "天" : b.StockUnit == 6 ? "小时" : b.StockUnit == 7 ? "分钟" : b.StockUnit == 8 ? "周" : b.StockUnit == 9 ? "月" : "",
                                                     MaxInventory = b.MaxInventory,
                                                     MinInventory = b.MinInventory,
                                                     SaveInventory = b.SaveInventory,
                                                     InventoryStore = b.InventoryStore,
                                                     InboundOrderNum = a.InboundOrderNum,
                                                     CreateId = a.CreateId,
                                                     CreateDate = a.CreateDate.ToString("yyyy-MM-dd HH:mm:ss"),
                                                     UpdateId = a.UpdateId,
                                                     UpdateDate = a.UpdateDate,
                                                     CreateName = a.CreateName,
                                                     UpdateName = a.UpdateName,
                                                 }).ToList();
                }

                result.Item= info;
                return Task.FromResult(result);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
